package com.zw.mes.module.tecdrawsign.controller;


import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.zw.mes.base.BaseController;
import com.zw.mes.common.JsonResult;
import com.zw.mes.entity.technology.DrawSign;
import com.zw.mes.entity.technology.DsnCoatSheet;
import com.zw.mes.entity.technology.Result;
import com.zw.mes.entity.technology.Tecard;
import com.zw.mes.module.cost.model.DictCommon;
import com.zw.mes.module.cost.service.DictCommonService;
import com.zw.mes.module.tecdrawsign.model.DictEnum;
import com.zw.mes.module.tecdrawsign.service.DrawSignService;
import com.zw.mes.module.tecdrawsign.service.PaintingService;
import com.zw.mes.util.FileUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

import static com.zw.mes.constant.Constant.GROUP_SPECTYPE;
import static com.zw.mes.constant.Constant.GROUP_WORKSHOP;

/**
 * @author mayongfeng
 * @version 1.0.0
 * @ClassName PurchasedController.java
 * @Description TODO
 * @createTime 2021年01月29日 09:06:00
 */
@Controller
@RequestMapping("/painting")
@SuppressWarnings("all")
public class PaintingController extends BaseController {

    @Autowired
    private DictCommonService dictCommonService;
    @Autowired
    private PaintingService paintingService;

    /**
     * @Author mayongfeng
     * @Date 15:39 2021/5/13
     * @Description 查询涂装明细
     *
     **/
    @GetMapping
    public String checkPage(String proCode, Model model) {
        model.addAttribute("special", dictCommonService.findDictByGroupName(GROUP_SPECTYPE));
        return "technology/sign/painting";
    }

    /**
     * @Author mayongfeng
     * @Date 10:52 2020/11/26
     * @Description 查询生产运营部所有的图纸变更记录
     *
     **/
    @ResponseBody
    @PostMapping("coat/queryList")
    public JsonResult getAll(DsnCoatSheet dsnCoatSheet){
        startPage();
        List<DsnCoatSheet> drawChaRecords = paintingService.getAll(dsnCoatSheet);
        return jsonResult(drawChaRecords);
    }

    /**
     * 查看功能
     */
    @GetMapping("coat/view/{id}")
    public String info(@PathVariable("id") Integer coatId, Model model) {
        DsnCoatSheet coatSheet = paintingService.findDsnCoatById(coatId);
        model.addAttribute("dsnCoatSheet", coatSheet);
        if (coatSheet.getSheetType() == 1) {
            model.addAttribute("coatDetails", paintingService.findCoatDetailBySheetId(coatId));
            return "technology/sign/coat";
        } else {
            model.addAttribute("oldDsnCoatSheet", paintingService.findDsnCoatById(coatSheet.getSheetId()));
            model.addAttribute("coatDetails", paintingService.findCoatDetailChangeBySheetId(coatId));
            return "technology/sign/coatChange";
        }
    }
    @GetMapping("/downloadProjectFileCoat/{id}")
    public void downloadProjectFileCoat(
            @PathVariable("id") Integer id, HttpServletResponse httpServletResponse) {
        try {
            DsnCoatSheet dsnCoatSheet = paintingService.getDsnCoatSheetByid(id);
            String filename = dsnCoatSheet.getUrl();
            filename = filename.replace("/files/", "e:/pdm/upload/");

            FileUtils.downloadFile(filename, httpServletResponse);

        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }
    @GetMapping("/downloadProjectFileCoatChange/{id}")
    public void downloadProjectFileCoatChange(
            @PathVariable("id") Integer id, HttpServletResponse httpServletResponse) {
        try {
            DsnCoatSheet dsnCoatSheet = paintingService.getDsnCoatSheetChangeByid(id);
            String filename = dsnCoatSheet.getAfterUrl();
            filename = filename.replace("/files/", "e:/pdm/upload/");

            FileUtils.downloadFile(filename, httpServletResponse);

        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }
    @GetMapping("/downloadProjectFileCoatOld/{id}")
    public void downloadProjectFileCoatOld(
            @PathVariable("id") Integer id, HttpServletResponse httpServletResponse) {
        try {
            DsnCoatSheet dsnCoatSheet = paintingService.getDsnCoatSheetChangeByid(id);
            String filename = dsnCoatSheet.getUrl();
            filename = filename.replace("/files/", "e:/pdm/upload/");

            FileUtils.downloadFile(filename, httpServletResponse);

        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }
    /**
     * 查看功能
     */
    @ResponseBody
    @PostMapping("coat/receive")
    public JsonResult receiveCoat( String dsnCoatSheets) {
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm").create();
        DsnCoatSheet dsnCoatSheet = gson.fromJson(dsnCoatSheets, DsnCoatSheet.class);
        int i = paintingService.receiveCoat(dsnCoatSheet);
        return jsonResult(i);
    }


}
